<template>
  <div id="app">
    <el-card shadow="hover">
      {{condition}}
      <!--条件查询-->
      <el-form :inline="true" size="medium" :model="condition" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="condition.name" placeholder="员工姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="condition.addr" placeholder="员工住址"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="condition.deptId" placeholder="所在部门">
            <el-option label="所有部门" value="0"></el-option>
            <el-option :label="c.deptName" :value="c.deptId" v-for="c in deptList" :key="c.deptId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="conditionSearch" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="openAddEmployee" icon="el-icon-plus">添加员工</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" icon="el-icon-remove" plain @click="handleDelete">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="employeeList"
          style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column
            type="selection">
        </el-table-column>
        <el-table-column
            label="工号"
            prop="id"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="姓名"
            align="center"
            prop="name">
        </el-table-column>
        <el-table-column
            label="性别"
            align="center"
            prop="sex">
        </el-table-column>
        <el-table-column
            label="年龄"
            align="center"
            prop="age">
        </el-table-column>
        <el-table-column
            label="住址"
            align="center"
            prop="addr">
        </el-table-column>
        <el-table-column
            label="所在部门"
            align="center"
            prop="deptName">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="small"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{employee}}
      <el-form :model="employee" :rules="rules" ref="employee">
        <el-form-item prop="name" label="员工姓名" :label-width="formLabelWidth">
          <el-input v-model="employee.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="员工性别" :label-width="formLabelWidth">
          <el-radio v-model="employee.sex" label="男">男</el-radio>
          <el-radio v-model="employee.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item prop="age" label="员工年龄" :label-width="formLabelWidth">
          <el-input v-model.number="employee.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="addr" label="员工住址" :label-width="formLabelWidth">
          <el-input v-model="employee.addr" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="cid" label="所在部门" :label-width="formLabelWidth" style="width: 1200px">
          <el-select v-model="employee.deptId" placeholder="请选择所在部门" >
            <el-option label="所有部门" value="0"></el-option>
            <el-option :label="c.deptName" :value="c.deptId" v-for="c in deptList" :key="c.deptId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEmployee">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import employee from "@/api/employee";
import dept from "@/api/dept";
export default {
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        age: [
            {   required: true, message: '请输入员工年龄(数字)', trigger: 'blur' },
            {   type: 'number',min: 0, max: 150, message: '年龄在0到150岁之间', trigger: 'blur'},
        ],
        addr: [
          { required: true, message: '请输入员工住址', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      },
      selected: [],
      condition: {},
      employee:{},
      deptList: [],
      employeeList: [],      // 当前页的所有员工信息集合
      page:1,               // 当前页
      pageSize:5,           // 每页大小
      total:0,            // 总记录数
      title: '添加员工',
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  created() {
    this.conditionSearch()
    this.findDept()

  },
  methods: {
    handleSelectionChange(rows){
      this.selected = []
      rows.forEach(row => {
        this.selected.push(row.id)
      })
      console.log(this.selected)
    },
    async findDept() {
      const res = await dept.findDept()
      this.deptList = res.data
    },

    async conditionSearch() {
      const res = await employee.search(this.page, this.pageSize, this.condition)
      this.employeeList = res.data.records
      this.total = res.data.total
    },
    openAddEmployee(){
      this.employee = {}
      this.title = '添加员工'
      this.dialogFormVisible = true
    },
    async addEmployee() {
      this.$refs.employee.validate(async valid => {
        if (!valid) {
          return false
        }
        if(!this.employee.id) {
          const res = await employee.save(this.employee)
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.dialogFormVisible = false
            this.employee = {}
            await this.conditionSearch()
          } else {
            this.$message.error(res.data.message)
          }
        }else{
          const res = await employee.update(this.employee)
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.dialogFormVisible = false
            this.employee = {}
            await this.conditionSearch()
          }else{
            this.$message.error(res.data.message)
          }
        }
      })
    },
    async getEmployeeById(id) {
      const res = await employee.findById(id)
      this.employee = res.data
    },
    handleEdit(index, row) {
      console.log(row)
      this.employee = row
      this.title = '修改员工'
      this.dialogFormVisible = true
    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if(row) {
          this.selected = []
          this.selected.push(row.id)
        }
        if(this.selected.length > 0){
          const res = await employee.deleteByIds(this.selected)
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            await this.conditionSearch()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            })
          }
        }
        else {
          this.$message({
            type: 'error',
            message: '请选择要删除的数据!'
          })
        }
        this.selected = []
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      this.pageSize = val;
      this.conditionSearch()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      this.page = val;
      this.conditionSearch()
    }
  }
}
</script>
<style>

</style>
